package controllers

import (
	"errors"
	"fmt"
	"gintest/database"
	"gintest/models"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"io/ioutil"
	"net/http"
)

// CreateProduct 创建产品
func CreateProduct(c *gin.Context) {
	var product models.Product
	if err := c.ShouldBindJSON(&product); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	db := database.GetDB()
	if err := db.Create(&product).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Create failed"})
		return
	}

	c.JSON(http.StatusOK, product)
}

// GetProducts 获取所有产品
func GetProducts(c *gin.Context) {
	var products []models.Product
	db := database.GetDB()
	db.Find(&products)

	c.JSON(http.StatusOK, products)
}

// GetProduct 根据 ID 获取产品
func GetProduct(c *gin.Context) {
	id := c.Param("id")
	var product models.Product
	db := database.GetDB()

	if err := db.First(&product, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Product not found"})
		return
	}

	c.JSON(http.StatusOK, product)
}

// UpdateProduct 更新产品
func UpdateProduct(c *gin.Context) {
	id := c.Param("id")
	db := database.GetDB()

	var product models.Product
	if err := db.First(&product, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Product not found"})
		return
	}

	if err := c.ShouldBindJSON(&product); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	db.Save(&product)
	c.JSON(http.StatusOK, product)
}

// DeleteProduct 删除产品
func DeleteProduct(c *gin.Context) {
	id := c.Param("id")
	db := database.GetDB()

	if err := db.Delete(&models.Product{}, id).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Delete failed"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Deleted successfully"})
}

type formA struct {
}

type formB struct {
}

// GetAll 获取全部请求
func GetAll(c *gin.Context) {
	fmt.Println("get all")
	objA := formA{}
	body, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"})
		return
	}

	// 打印请求体内容
	fmt.Println("Request Body:", string(body))
	if errA := c.ShouldBind(&objA); errA == nil {
		fmt.Println(objA)
		c.JSON(http.StatusOK, gin.H{"message": "Deleted successfully"})
	}
}
func Va(c *gin.Context) {
	var json models.Login // 创建 models.Login 类型的实例
	if err := c.ShouldBindJSON(&json); err != nil {
		// 验证失败，获取详细错误信息
		var validationErrors validator.ValidationErrors
		errors.As(err, &validationErrors)

		// 遍历错误并构建中文错误信息
		var errorMessages []string
		for _, e := range validationErrors {
			// 根据字段和验证规则返回自定义的错误信息
			switch e.Field() {
			case "User":
				errorMessages = append(errorMessages, "用户名不能为空")
			case "Password":
				errorMessages = append(errorMessages, "密码不能为空")
			default:
				errorMessages = append(errorMessages, e.Error())
			}
		}

		// 返回中文错误信息
		c.JSON(http.StatusBadRequest, gin.H{"error": errorMessages})
		return
	}
}
func va2(c *gin.Context) {
	var json models.Login // 创建 models.Login 类型的实例
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 进一步的处理逻辑
	c.JSON(http.StatusOK, gin.H{"message": "Login successful", "data": json})
}
